package com.qiuling.iot.service.impl;

import com.qiuling.iot.entity.SysDept;
import com.qiuling.iot.entity.SysUser;
import com.qiuling.iot.mapper.SysDeptMapper;
import com.qiuling.iot.mapper.SysUserMapper;
import com.qiuling.iot.service.HomeService;
import com.qiuling.iot.service.PermissionService;
import com.qiuling.iot.vo.resp.HomeRespVO;
import com.qiuling.iot.vo.resp.PermissionRespNodeVO;
import com.qiuling.iot.vo.resp.UserInfoRespVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author：zhangqiang
 * @Description：
 * @Date：Created in 16:45 2020/2/11
 * @Modified By：
 */
@Service
@Transactional(readOnly = true)
public class HomeServiceImpl implements HomeService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private PermissionService permissionService;

    @Override
    public HomeRespVO getHome(String userId) {
        HomeRespVO homeRespVO = new HomeRespVO();
        List<PermissionRespNodeVO> list = permissionService.permissionTreeList(userId);
        homeRespVO.setMenus(list);
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
        UserInfoRespVO vo = new UserInfoRespVO();
        if (sysUser != null) {
            BeanUtils.copyProperties(sysUser, vo);
            if(StringUtils.isNotBlank(sysUser.getDeptId())) {
                SysDept sysDept = sysDeptMapper.selectByPrimaryKey(sysUser.getDeptId());
                vo.setDeptName(sysDept.getName());
            }

        }
        homeRespVO.setUserInfoVO(vo);
        return homeRespVO;
    }
}
